package com.ly.quickdev.library.fragment;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DevBaseFragment extends Fragment implements OnClickListener {
	public String getTextViewText(int id) {
		TextView tv = findViewById(id);
		return tv == null ? "" : tv.getText().toString();
	}

	public void setTextViewText(int id, CharSequence text) {
		TextView tv = findViewById(id);
		if (tv != null) {
			tv.setText(text);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(getLayoutId(), container, false);
	}

	public int getLayoutId() {
		return 0;
	}

	@SuppressWarnings("unchecked")
	public <T extends View> T findViewById(int id) {
		return (T) getView().findViewById(id);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	public String getTitle() {
		return getTag();
	}

	public void showToast(int resId) {
		if (getActivity() != null)
			Toast.makeText(getActivity(), resId, Toast.LENGTH_SHORT).show();
	}

	public void showToast(CharSequence text) {
		if (getActivity() != null)
			Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
	}

	public void setTitle(CharSequence title) {
		final ActionBar actionBar = ((ActionBarActivity) getActivity())
				.getSupportActionBar();
		if (actionBar != null) {
			actionBar.setTitle(title);
		}
	}

	public ProgressDialog mProgressDialog;

	public void showProgressDialog(String msg) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(getActivity());
			mProgressDialog.setCancelable(false);
		}
		if (TextUtils.isEmpty(msg)) {
			msg = "正在加载数据...";
		}
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}

	public void cancelProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.cancel();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public Dialog showNormalDialog(String title, String msg, String btnString,
			android.content.DialogInterface.OnClickListener l) {
		Builder mBuilder = new Builder(getActivity());
		mBuilder.setTitle(title);
		mBuilder.setMessage(msg);
		mBuilder.setPositiveButton(btnString, l);
		Dialog d = mBuilder.create();
		d.show();
		return d;
	}
}
